CHARTS
Photo by Yogendra Singh on Unsplash
You cannot predict the outcome of human development.
All you can do is like a farmer create the conditions under which it will begin to flourish…
— Ken Robinson
df <- read.csv('archetypes/hdi/hdi-2020.csv', encoding = "UTF-8")
df
df$REGION <- countrycode(df$Country, origin = 'country.name', destination = 'region')
df_wrangle <- df
# The pca function can only take numeric values,
# so we convert Country column to be rownames
df_wrangle <- column_to_rownames(df_wrangle, var = "Country")
# Convert string to numeric
df_wrangle$Gross.national.income..GNI..per.capita <- parse_number(df_wrangle$Gross.national.income..GNI..per.capita)
# Remove HDI ranks to use key indicators only
df_wrangle <- df_wrangle %>% select(Life.expectancy.at.birth, Expected.years.of.schooling, Mean.years.of.schooling, Gross.national.income..GNI..per.capita, )
df_wrangle
# fit a principal components model
df_pca <- prcomp(x = df_wrangle,
center = TRUE,
scale = TRUE)
df_pca
## Standard deviations (1, .., p=4):
## [1] 1.7879597 0.6224001 0.4794378 0.4312281
##
## Rotation (n x k) = (4 x 4):
## PC1 PC2 PC3
## Life.expectancy.at.birth -0.5102770 -0.1092925 0.79381958
## Expected.years.of.schooling -0.5107632 -0.3749363 -0.07710668
## Mean.years.of.schooling -0.5087707 -0.3121884 -0.58572160
## Gross.national.income..GNI..per.capita -0.4689250 0.8660348 -0.14434419
## PC4
## Life.expectancy.at.birth 0.31228678
## Expected.years.of.schooling -0.76980405
## Mean.years.of.schooling 0.54828911
## Gross.national.income..GNI..per.capita -0.09621713
# review computed results
# var <- get_pca_var(df_pca)
# var
# head(var$coord)
# head(var$cor)
# head(var$cos2)
# head(var$contrib)
# ind <- get_pca_ind(df_pca)
# ind
# head(ind$coord)
# head(ind$cos2)
# head(ind$contrib)
# fviz_pca function produces a ggplot2 graph.
v1 <- fviz_pca(df_pca, repel = TRUE, labelsize = 3) +
theme_bw() +
labs(title = "HDI Biplot")
# Draw plot
girafe(ggobj = v1, width_svg = 13, height_svg = 7,
options = list(opts_sizing(rescale = TRUE, width = 1.0)))